import tkinter as tk
from tkinter import ttk
import pyautogui
import time

import pyperclip
from openai import OpenAI

# DeepSeek API 相关信息
DEEPSEEK_API_KEY = ""  # 替换为实际的 API Key
DEEPSEEK_API_BASE_URL = "https://api.deepseek.com"

# 初始化 OpenAI 客户端
client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url=DEEPSEEK_API_BASE_URL)


def get_ai_reply(user_input):
    try:
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[
                {"role": "system", "content": "You are a helpful assistant"},
                {"role": "user", "content": user_input}
            ],
            stream=False
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"调用 API 出错: {str(e)}"


def send_message():
    user_input = entry.get()
    if not user_input:
        result_label.config(text="请输入消息后再发送。", foreground="red")
        return
    result_label.config(text="正在获取回复，请稍候...", foreground="blue")
    root.update()  # 强制更新界面显示提示信息

    # 获取 AI 回复
    reply = get_ai_reply(user_input)
    # 显示回复
    result_label.config(text=f"回复成功！", foreground="black")

    # 模拟点击输入框，使用获取的坐标
    x, y = pyautogui.position()
    pyautogui.click(x, y)

    message = "天狼AI: " + reply
    print(message)
    pyperclip.copy(message)
    pyautogui.hotkey('ctrl', 'v')
    # 模拟按下回车键发送消息（这里假设发送按键是回车键）
    pyautogui.press('enter')


# 创建主窗口
root = tk.Tk()
root.title("天狼智能聊天端")
root.geometry("500x300")  # 设置窗口大小
root.configure(bg="#f0f0f0")  # 设置窗口背景颜色

input_frame = ttk.Frame(root)
input_frame.pack(pady=20, padx=20, fill=tk.X)

input_label = ttk.Label(input_frame, text="请输入消息:", font=("Arial", 12))
input_label.pack(side=tk.LEFT)

entry = ttk.Entry(input_frame, width=30, font=("Arial", 12))
entry.pack(side=tk.LEFT, padx=10, expand=True, fill=tk.X)

button_frame = ttk.Frame(root)
button_frame.pack(pady=10)

send_button = ttk.Button(button_frame, text="发送", command=send_message, style="TButton")
send_button.pack(pady=10)

result_frame = ttk.Frame(root)
result_frame.pack(pady=20, padx=20, fill=tk.X)

result_label = ttk.Label(result_frame, text="", font=("Arial", 12), wraplength=460)
result_label.pack()

# 设置按钮样式
style = ttk.Style()
style.configure("TButton", font=("Arial", 12), padding=10)

# 运行主循环
root.mainloop()